<?php
	if ( !isset($_GET["p"]) and !isset($_GET["q"]) ) {
		$page = "link";
		$meth = "page";
	} else if ( isset($_GET["p"]) and ($_GET["p"] == "link" or $_GET["p"] == "hops" or $_GET["p"] == "avg" or $_GET["p"] == "about") ) {
		$page = $_GET["p"];
		$meth = "page";
	} else if ( isset($_GET["q"]) and ($_GET["q"] == "link" or $_GET["q"] == "hops" or $_GET["q"] == "avg") ) {
		$page = $_GET["q"];
		$meth = "query";
	} else {
		$meth = "error";
		// Error -> unknown query
	}

	if ( $meth == "page" and $page != "error" and $page != "about" ) {
	    if ( isset($_GET["source"]) ) $source = $_GET["source"];
    	else $source = "Woody Allen";

	if ( $page == "link" ) {
	    	if ( isset($_GET["target"]) ) $target = $_GET["target"];
	    	else $target = "";
		} else if ( $page == "hops" ) {
	    	if ( isset($_GET["hops"]) ) $target = $_GET["hops"];
	    	else $target = "";
		}

		if ( isset($_GET["minyear"]) ) $miny = $_GET["minyear"];
		else $miny = 1850;

		if ( isset($_GET["maxyear"]) ) $maxy = $_GET["maxyear"];
		else $maxy = 2050;

		if ( isset($_GET["work"]) && !is_array($_GET["work"]) )
			unset($_GET["work"]);

		if ( isset($_GET["media"]) && !is_array($_GET["media"]) )
			unset($_GET["media"]);

		if ( isset($_GET["genre"]) && !is_array($_GET["genre"]) )
			unset($_GET["genre"]);
	} else if ( $meth == "query" ) {
		if ( !isset($_GET["source"]) || trim($_GET["source"]) == "" ) {
			header("Location: ./?p=$page");
			exit(0);
		} else $source = htmlspecialchars(trim($_GET["source"]), ENT_QUOTES, "UTF-8");

		if ( $page == "link" ) {
			if ( !isset($_GET["target"]) || trim($_GET["target"]) == "" ) { 
				header("Location: ./?p=$page");
				exit(0);
			} else $target = htmlspecialchars(trim($_GET["target"]), ENT_QUOTES, "UTF-8");
		} else if ( $page == "hops" ) {
			if ( !isset($_GET["hops"]) || trim($_GET["hops"]) == "" ) { 
				header("Location: ./?p=$page");
				exit(0);
			} else $hops = htmlspecialchars(trim($_GET["hops"]), ENT_QUOTES, "UTF-8");			
		}

		if ( !isset($_GET["minyear"]) || trim($_GET["minyear"]) == "" ) $minyear = 1850;
		else $minyear = htmlspecialchars(trim($_GET["minyear"]), ENT_QUOTES, "UTF-8");
		if ( !isset($_GET["maxyear"]) || trim($_GET["maxyear"]) == "" ) $maxyear = 2050;
		else $maxyear = htmlspecialchars(trim($_GET["maxyear"]), ENT_QUOTES, "UTF-8");

		$suspended = isset($_GET["suspended"]) ? "true" : "false";

		$exact_source = isset($_GET["source_exact"]) ? "true" : "false";
		$exact_target = isset($_GET["target_exact"]) ? "true" : "false";

		if ( !isset($_GET["media"]) ) $medias = "<media>All</media>";
		else if ( !is_array($_GET["media"]) ) {
			// Error -> bad querry format (media is not array)
		} else {
			$medias = "";
			foreach ( $_GET["media"] as $m )
				$medias .= "<media>".htmlspecialchars($m)."</media>";
		}

		if ( !isset($_GET["genre"]) ) $genres = "<genre>All</genre>";
		else if ( !is_array($_GET["genre"]) ) {
			// Error -> bad query format (genre is not array)
		} else {
			$genres = "";
			foreach ( $_GET["genre"] as $g )
				$genres .= "<genre>".htmlspecialchars($g)."</genre>";
		}

		if ( !isset($_GET["work"]) ) $works = "<work>All</work>";
		else if ( !is_array($_GET["work"]) ) {
			// Error -> bad query format (work is not array)
		} else {
			$works = "";
			foreach ( $_GET["work"] as $w )
				$works .= "<work>".htmlspecialchars($w)."</work>";
		}
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Oracle of Woody Allen</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" media="all" type="text/css" href="./style.css" />
<link rel="shortcut icon" href="./favicon.png" type="image/png" />
</head>
<body>
<div id="all">
<div id="left">
<a href="./"><img src="./woody-allen.png" alt="Woody Allen" width="132" height="160"/></a>
<ul class="menu">
<li><a href="./?p=link">Link two persons</a></li>
<li><a href="./?p=hops">People at a given number of hops</a></li>
<li><a href="./?p=avg">Average number of hops</a></li>
<li><a href="./?p=about">About</a></li>
</ul>
</div>

<div id="right">
<div id="main-title">The Oracle of Woody Allen</div>
<div id="main-content">
<?php
	if ( $meth == "error" ) require("error.html");
	else if ( $meth == "page" ) {
		if ( $page == "about" ) require("about.html");
		else require("form.php");
	} else  {
		require_once("connection.php");
		require_once("xmldistance.php");
		require_once("xmlcenter.php");
		require_once("xmlhops.php");
		require_once("log.php");

		$conn = new OracleConnection();
		$request = "";

		if ( $page == "link" ) {
		  $request  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		  $request .= "<request xmlns=\"http://www.jpuigcerver.net/allen/xml/request.xsd\" version=\"0.3\">";
		  $request .= "<distance>";
		  $request .= "<source name=\"$source\" exact=\"$exact_source\" />";	
		  $request .= "<target name=\"$target\" exact=\"$exact_target\" />";
		  $request .= "</distance>";
		  $request .= "<filter>";
		  $request .= "<min>$minyear</min>";
		  $request .= "<max>$maxyear</max>";
		  $request .= "<suspended>$suspended</suspended>";
		  $request .= "<medias>$medias</medias>";
		  $request .= "<genres>$genres</genres>";
		  $request .= "<works>$works</works>";
		  $request .= "</filter>";
		  $request .= "</request>";
		} else if ( $page == "hops" ) {
		  $request  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		  $request .= "<request xmlns=\"http://www.jpuigcerver.net/allen/xml/request.xsd\" version=\"0.3\">";
		  $request .= "<distance_n hops=\"$hops\">";
		  $request .= "<source name=\"$source\" exact=\"$exact_source\" />";
		  $request .= "</distance_n>";
		  $request .= "<filter>";
		  $request .= "<min>$minyear</min>";
		  $request .= "<max>$maxyear</max>";
		  $request .= "<suspended>$suspended</suspended>";
		  $request .= "<medias>$medias</medias>";
		  $request .= "<genres>$genres</genres>";
		  $request .= "<works>$works</works>";
		  $request .= "</filter>";
		  $request .= "</request>";
		} else {
		  $request  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		  $request .= "<request xmlns=\"http://www.jpuigcerver.net/allen/xml/request.xsd\" version=\"0.3\">";
		  $request .= "<center>";
		  $request .= "<source name=\"$source\" exact=\"$exact_source\" />";
		  $request .= "</center>";
		  $request .= "<filter>";
		  $request .= "<min>$minyear</min>";
		  $request .= "<max>$maxyear</max>";
		  $request .= "<suspended>$suspended</suspended>";
		  $request .= "<medias>$medias</medias>";
		  $request .= "<genres>$genres</genres>";
		  $request .= "<works>$works</works>";
		  $request .= "</filter>";
		  $request .= "</request>";
		}

		try {
			$resp = $conn->query($request);
			if ( $page == "link" ) $xml_resp = new XMLDistanceParser($resp);
			else if ( $page == "hops" ) $xml_resp = new XMLHopsParser($resp);
			else $xml_resp = new XMLCenterParser($resp);
			print_log("Request from " . $_SERVER['REMOTE_ADDR'] . ":\n" . $request . "\n");
			echo $xml_resp->getResponse($_GET, $page);
		} catch ( Exception $e ) {
			echo "<p>".$e->getMessage()."</p>";
		}
	}
?>
</div>
</div>
<div id="clear" />
</div>
<div id="footer">
Copyright (C) 2010-2011 Joan Puigcerver Pérez &lt; joapuipe at gmail dot com &gt;<br/>
Verbatim copying and distribution of this entire web site are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.
</div>
</body>
</html>
